package org.example.me.handler;

import lombok.extern.slf4j.Slf4j;
import org.example.me.enums.ExcelTypeEnum;
import org.example.me.handler.template.AbstractCommonExportTemplate;
import org.example.me.handler.context.ExcelContextExtend;
import org.example.me.handler.context.FileContext;
import org.example.me.handler.context.SheetCreator;
import org.example.me.dao.DataGenerator;
import org.example.me.model.Order;
import org.example.me.model.User;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * 多Sheet Excel导出处理器
 * 负责将不同类型的数据导出到同一个Excel文件的不同Sheet中
 */
@Slf4j
@Component
public class MultiSheetExportHandler extends AbstractCommonExportTemplate {

    /**
     * 获取处理器对应的Excel类型
     *
     * @return Excel类型枚举值
     */
    public ExcelTypeEnum getExcelType() {
        return ExcelTypeEnum.MULTI_SHEET;
    }

    /**
     * 导出多Sheet数据到Excel文件
     *
     * @param excelContext Excel上下文信息
     */
    @Override
    protected void export(ExcelContextExtend excelContext) {
        // 初始化用户导出临时目录
        List<Order> orders = DataGenerator.generateOrderData();
        List<User> users = DataGenerator.generateUserData();

        // 写法1：使用doWriteData方法自动管理文件和Sheet
        doWriteData(excelContext, "文件1", users, SheetCreator.builder().sheetName("用户").tClass(User.class).build());
        doWriteData(excelContext, "文件1", orders, SheetCreator.builder().sheetName("订单").tClass(Order.class).build());

        // 写法2：手动管理文件和Sheet
//        FileContext fileContext = initFileContext(excelContext.getExcelContext(), "文件3");
//        excelContext.addFileContext(fileContext);
//        WriteSheet writeSheet = initWriteSheet(SheetCreator.builder().sheetName("用户").tClass(Order.class).build());
//        WriteSheet writeSheet2 = initWriteSheet(SheetCreator.builder().sheetName("订单").tClass(Order.class).build());
//        fileContext.addWriteSheet(writeSheet);
//        fileContext.addWriteSheet(writeSheet2);
//        doWriteData(fileContext, "用户", users);
//        doWriteData(fileContext, "订单", orders);

        SheetCreator build = SheetCreator.builder().sheetName("用户").tClass(Order.class).build();
        SheetCreator build1 = SheetCreator.builder().sheetName("订单").tClass(Order.class).build();
        List<SheetCreator> writeSheets = new ArrayList<>();
        writeSheets.add(build);
        writeSheets.add(build1);
        FileContext fileContext = initFileContext(excelContext.getExcelContext(), "文件3", writeSheets);
        doWriteData(fileContext, "用户", users);
        doWriteData(fileContext, "订单", orders);
    }
}